package com.huike.clues.service.impl;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.domain.vo.RecordVo;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.TbClueTrackRecordService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.unit.DataUnit;

import java.util.List;
import java.util.Map;

/**
 * author:
 * Date:2022/3/3115:14
 */
@Service
public class TbClueTrackRecordServiceImpl implements TbClueTrackRecordService {
    @Autowired
    private TbClueTrackRecordMapper clueTrackRecordMapper;

    @Autowired
    private  SysDictDataMapper sysDictDataMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    @Autowired
    private ITbClueService iTbClueService;
    @Override
    public void insertTbClueTrackRecord(ClueTrackRecordVo clueTrackRecord) {
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        tbClueTrackRecord.setClueId(clueTrackRecord.getClueId());
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecord.setSubject(clueTrackRecord.getSubject());
        tbClueTrackRecord.setRecord(clueTrackRecord.getRecord());
        tbClueTrackRecord.setLevel(clueTrackRecord.getLevel());
        tbClueTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbClueTrackRecord.setType("0");
        tbClueTrackRecord.setNextTime(clueTrackRecord.getNextTime());

        clueTrackRecordMapper.insertTbClueTrackRecord(tbClueTrackRecord);
    }

    @Override
    public RecordVo selectClueTrackRecordById(TbClueTrackRecord clueTrackRecord) {
        RecordVo recordVo = new RecordVo();
        Long clueId = clueTrackRecord.getClueId();
        recordVo.setTotal(clueTrackRecordMapper.selectRecordCount(clueId));
        List<TbClueTrackRecord> maps = clueTrackRecordMapper.selectClueTrackRecordList(clueTrackRecord);
        recordVo.setRows(maps);

        return  recordVo;

    }

    @Override
    public void updateReason(Long clueId, Map<String, Object> map) {
        TbClue tbClue = tbClueMapper.selectTbClueById(clueId);
        int falseCount = tbClue.getFalseCount();
        if (falseCount>=2){
             tbClueMapper.removeClueByFalseClue(clueId);
             return;
        }
        tbClue.setFalseCount(tbClue.getFalseCount()+1);
        tbClue.setStatus(TbClue.StatusType.FALSE.getValue());
        iTbClueService.updateTbClue(tbClue);

        String label = sysDictDataMapper.selectDictLabel("reasons_for_reporting", (String) map.get("reason"));
        TbClueTrackRecord tbClueTrackRecord = new TbClueTrackRecord();
        tbClueTrackRecord.setFalseReason(label);
        tbClueTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbClueTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbClueTrackRecord.setRecord((String) map.get("remark"));
        clueTrackRecordMapper.insertTbClueTrackRecord(tbClueTrackRecord);
    }
}
